 package handling.world.family;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.world.WorldBroadcastService;
 import handling.world.WorldFamilyService;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.MaplePacketCreator;
 import tools.packet.FamilyPacket;



 public final class MapleFamily
   implements Serializable
 {
   public static final long serialVersionUID = 6322150443228168192L;
/*  29 */   private static final Logger log = LogManager.getLogger(MapleFamily.class);

/*  31 */   private final Map<Integer, MapleFamilyCharacter> members = new ConcurrentHashMap<>();
/*  32 */   private String leadername = null;

   private String notice;
   private int id;

   public MapleFamily(int familyid) {
/*  38 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  39 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM families WHERE familyid = ?");
/*  40 */       ps.setInt(1, familyid);
/*  41 */       ResultSet rs = ps.executeQuery();

/*  43 */       if (!rs.next()) {
/*  44 */         rs.close();
/*  45 */         ps.close();
/*  46 */         this.id = -1;
/*  47 */         this.proper = false;
         return;
       }
/*  50 */       this.id = familyid;
/*  51 */       this.leaderid = rs.getInt("leaderid");
/*  52 */       this.notice = rs.getString("notice");
/*  53 */       rs.close();
/*  54 */       ps.close();

/*  56 */       ps = con.prepareStatement("SELECT id, name, level, job, seniorid, junior1, junior2, currentrep, totalrep FROM characters WHERE familyid = ?", 1008);
/*  57 */       ps.setInt(1, familyid);
/*  58 */       rs = ps.executeQuery();
/*  59 */       while (rs.next()) {
/*  60 */         if (rs.getInt("id") == this.leaderid) {
/*  61 */           this.leadername = rs.getString("name");
         }
/*  63 */         this.members.put(Integer.valueOf(rs.getInt("id")), new MapleFamilyCharacter(rs.getInt("id"), rs.getShort("level"), rs.getString("name"), -1, rs.getInt("job"), familyid, rs.getInt("seniorid"), rs.getInt("junior1"), rs.getInt("junior2"), rs.getInt("currentrep"), rs.getInt("totalrep"), false));
       }
/*  65 */       rs.close();
/*  66 */       ps.close();

/*  68 */       if (this.leadername == null || this.members.size() < 2) {
/*  69 */         System.err.println("Leader " + this.leaderid + " isn't in family " + this.id + ". Members: " + this.members.size() + ".  Impossible... family is disbanding.");
/*  70 */         writeToDB(true);
/*  71 */         this.proper = false;

         return;
       }
/*  75 */       for (MapleFamilyCharacter mfc : this.members.values()) {
/*  76 */         if (mfc.getJunior1() > 0 && (getMFC(mfc.getJunior1()) == null || mfc.getId() == mfc.getJunior1())) {
/*  77 */           mfc.setJunior1(0);
         }
/*  79 */         if (mfc.getJunior2() > 0 && (getMFC(mfc.getJunior2()) == null || mfc.getId() == mfc.getJunior2() || mfc.getJunior1() == mfc.getJunior2())) {
/*  80 */           mfc.setJunior2(0);
         }
/*  82 */         if (mfc.getSeniorId() > 0 && (getMFC(mfc.getSeniorId()) == null || mfc.getId() == mfc.getSeniorId())) {
/*  83 */           mfc.setSeniorId(0);
         }
/*  85 */         if (mfc.getJunior2() > 0 && mfc.getJunior1() <= 0) {
/*  86 */           mfc.setJunior1(mfc.getJunior2());
/*  87 */           mfc.setJunior2(0);
         }
/*  89 */         if (mfc.getJunior1() > 0) {
/*  90 */           MapleFamilyCharacter mfc2 = getMFC(mfc.getJunior1());
/*  91 */           if (mfc2.getJunior1() == mfc.getId()) {
/*  92 */             mfc2.setJunior1(0);
           }
/*  94 */           if (mfc2.getJunior2() == mfc.getId()) {
/*  95 */             mfc2.setJunior2(0);
           }
/*  97 */           if (mfc2.getSeniorId() != mfc.getId()) {
/*  98 */             mfc2.setSeniorId(mfc.getId());
           }
         }
/* 101 */         if (mfc.getJunior2() > 0) {
/* 102 */           MapleFamilyCharacter mfc2 = getMFC(mfc.getJunior2());
/* 103 */           if (mfc2.getJunior1() == mfc.getId()) {
/* 104 */             mfc2.setJunior1(0);
           }
/* 106 */           if (mfc2.getJunior2() == mfc.getId()) {
/* 107 */             mfc2.setJunior2(0);
           }
/* 109 */           if (mfc2.getSeniorId() != mfc.getId()) {
/* 110 */             mfc2.setSeniorId(mfc.getId());
           }
         }
       }
/* 114 */       resetPedigree();
/* 115 */       resetDescendants();
/* 116 */     } catch (SQLException se) {
/* 117 */       log.error("[MapleFamily] 从数据库中读取学院信息出错." + se);
     }
   }
   private int leaderid; private boolean proper = true, bDirty = false, changed = false;
   public static void loadAll() {
/* 122 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 123 */       PreparedStatement ps = con.prepareStatement("SELECT familyid FROM families");
/* 124 */       ResultSet rs = ps.executeQuery();
/* 125 */       while (rs.next()) {
/* 126 */         WorldFamilyService.getInstance().addLoadedFamily(new MapleFamily(rs.getInt("familyid")));
       }
/* 128 */       rs.close();
/* 129 */       ps.close();
/* 130 */     } catch (SQLException se) {
/* 131 */       log.error("[MapleFamily] 从数据库中读取学院信息出错." + se);
     }
   }

   public static void loadAll(Object toNotify) {
/* 136 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 137 */       boolean cont = false;
/* 138 */       PreparedStatement ps = con.prepareStatement("SELECT familyid FROM families");
/* 139 */       ResultSet rs = ps.executeQuery();
/* 140 */       while (rs.next()) {
/* 141 */         FamilyLoad.QueueFamilyForLoad(rs.getInt("familyid"));
/* 142 */         cont = true;
       }
/* 144 */       rs.close();
/* 145 */       ps.close();
/* 146 */       if (!cont) {
         return;
       }
/* 149 */     } catch (SQLException se) {
/* 150 */       log.error("[MapleFamily] 从数据库中读取学院信息出错." + se);
     }
/* 152 */     AtomicInteger FinishedThreads = new AtomicInteger(0);
/* 153 */     FamilyLoad.Execute(toNotify);
/* 154 */     synchronized (toNotify) {
       try {
/* 156 */         toNotify.wait();
/* 157 */       } catch (InterruptedException ex) {
/* 158 */         ex.printStackTrace();
       }
     }
/* 161 */     while (FinishedThreads.incrementAndGet() != 8) {
/* 162 */       synchronized (toNotify) {
         try {
/* 164 */           toNotify.wait();
/* 165 */         } catch (InterruptedException ex) {
/* 166 */           ex.printStackTrace();
         }
       }
     }
   }

   public static void setOfflineFamilyStatus(int familyid, int seniorid, int junior1, int junior2, int currentrep, int totalrep, int chrid) {
/* 173 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 174 */       PreparedStatement ps = con.prepareStatement("UPDATE characters SET familyid = ?, seniorid = ?, junior1 = ?, junior2 = ?, currentrep = ?, totalrep = ? WHERE id = ?");
/* 175 */       ps.setInt(1, familyid);
/* 176 */       ps.setInt(2, seniorid);
/* 177 */       ps.setInt(3, junior1);
/* 178 */       ps.setInt(4, junior2);
/* 179 */       ps.setInt(5, currentrep);
/* 180 */       ps.setInt(6, totalrep);
/* 181 */       ps.setInt(7, chrid);
/* 182 */       ps.execute();
/* 183 */       ps.close();
/* 184 */     } catch (SQLException se) {
/* 185 */       System.out.println("SQLException: " + se.getLocalizedMessage());
     }
   }

   public static int createFamily(int leaderId) {
/* 190 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 191 */       PreparedStatement ps = con.prepareStatement("INSERT INTO families (`leaderid`) VALUES (?)", 1);
/* 192 */       ps.setInt(1, leaderId);
/* 193 */       ps.executeUpdate();
/* 194 */       ResultSet rs = ps.getGeneratedKeys();
/* 195 */       if (!rs.next()) {
/* 196 */         rs.close();
/* 197 */         ps.close();
/* 198 */         return 0;
       }
/* 200 */       int ret = rs.getInt(1);
/* 201 */       rs.close();
/* 202 */       ps.close();
/* 203 */       return ret;
/* 204 */     } catch (Exception e) {
/* 205 */       log.error("[MapleFamily] 创建学院信息出错." + e);
/* 206 */       return 0;
     }
   }



   public static void mergeFamily(MapleFamily newfam, MapleFamily oldfam) {
/* 213 */     for (MapleFamilyCharacter mgc : oldfam.members.values()) {
/* 214 */       mgc.setFamilyId(newfam.getId());
/* 215 */       if (mgc.isOnline()) {
/* 216 */         WorldFamilyService.getInstance().setFamily(newfam.getId(), mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId());
       } else {
/* 218 */         setOfflineFamilyStatus(newfam.getId(), mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId());
       }
/* 220 */       newfam.members.put(Integer.valueOf(mgc.getId()), mgc);
/* 221 */       newfam.setOnline(mgc.getId(), mgc.isOnline(), mgc.getChannel());
     }
/* 223 */     newfam.resetPedigree();

/* 225 */     WorldFamilyService.getInstance().disbandFamily(oldfam.getId());
   }

   public void resetPedigree() {
/* 229 */     for (MapleFamilyCharacter mfc : this.members.values()) {
/* 230 */       mfc.resetPedigree(this);
     }
/* 232 */     this.bDirty = true;
   }

   public void resetDescendants() {
/* 236 */     MapleFamilyCharacter mfc = getMFC(this.leaderid);
/* 237 */     if (mfc != null) {
/* 238 */       mfc.resetDescendants(this);
     }
/* 240 */     this.bDirty = true;
   }

   public boolean isProper() {
/* 244 */     return this.proper;
   }

   public void writeToDB(boolean isDisband) {
/* 248 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 249 */       if (!isDisband) {
/* 250 */         if (this.changed) {
/* 251 */           PreparedStatement ps = con.prepareStatement("UPDATE families SET notice = ? WHERE familyid = ?");
/* 252 */           ps.setString(1, this.notice);
/* 253 */           ps.setInt(2, this.id);
/* 254 */           ps.execute();
/* 255 */           ps.close();
         }
/* 257 */         this.changed = false;
       } else {

/* 260 */         if (this.leadername == null || this.members.size() < 2) {
/* 261 */           broadcast(null, -1, FCOp.DISBAND, null);
         }

/* 264 */         PreparedStatement ps = con.prepareStatement("DELETE FROM families WHERE familyid = ?");
/* 265 */         ps.setInt(1, this.id);
/* 266 */         ps.execute();
/* 267 */         ps.close();
       }
/* 269 */     } catch (SQLException se) {
/* 270 */       log.error("[MapleFamily] 保存学院信息出错." + se);
     }
   }

   public int getId() {
/* 275 */     return this.id;
   }

   public int getLeaderId() {
/* 279 */     return this.leaderid;
   }

   public String getNotice() {
/* 283 */     if (this.notice == null) {
/* 284 */       return "";
     }
/* 286 */     return this.notice;
   }

   public void setNotice(String notice) {
/* 290 */     this.changed = true;
/* 291 */     this.notice = notice;
   }

   public String getLeaderName() {
/* 295 */     return this.leadername;
   }

   public void broadcast(byte[] packet, List<Integer> chrIds) {
/* 299 */     broadcast(packet, -1, FCOp.NONE, chrIds);
   }

   public void broadcast(byte[] packet, int exception, List<Integer> chrIds) {
/* 303 */     broadcast(packet, exception, FCOp.NONE, chrIds);
   }


   public void broadcast(byte[] packet, int exceptionId, FCOp bcop, List<Integer> chrIds) {
/* 308 */     buildNotifications();
/* 309 */     if (this.members.size() < 2) {
/* 310 */       this.bDirty = true;
       return;
     }
/* 313 */     for (MapleFamilyCharacter mgc : this.members.values()) {
/* 314 */       if (chrIds == null || chrIds.contains(Integer.valueOf(mgc.getId()))) {
/* 315 */         if (bcop == FCOp.DISBAND) {
/* 316 */           if (mgc.isOnline()) {
/* 317 */             WorldFamilyService.getInstance().setFamily(0, 0, 0, 0, mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId()); continue;
           }
/* 319 */           setOfflineFamilyStatus(0, 0, 0, 0, mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId()); continue;
         }
/* 321 */         if (mgc.isOnline() && mgc.getId() != exceptionId) {
/* 322 */           WorldBroadcastService.getInstance().sendFamilyPacket(mgc.getId(), packet, exceptionId, this.id);
         }
       }
     }
   }

   private void buildNotifications() {
/* 329 */     if (!this.bDirty) {
       return;
     }
/* 332 */     Iterator<Map.Entry<Integer, MapleFamilyCharacter>> toRemove = this.members.entrySet().iterator();
/* 333 */     while (toRemove.hasNext()) {
/* 334 */       MapleFamilyCharacter mfc = (MapleFamilyCharacter)((Map.Entry)toRemove.next()).getValue();
/* 335 */       if (mfc.getJunior1() > 0 && getMFC(mfc.getJunior1()) == null) {
/* 336 */         mfc.setJunior1(0);
       }
/* 338 */       if (mfc.getJunior2() > 0 && getMFC(mfc.getJunior2()) == null) {
/* 339 */         mfc.setJunior2(0);
       }
/* 341 */       if (mfc.getSeniorId() > 0 && getMFC(mfc.getSeniorId()) == null) {
/* 342 */         mfc.setSeniorId(0);
       }
/* 344 */       if (mfc.getFamilyId() != this.id) {
/* 345 */         toRemove.remove();
       }
     }
/* 348 */     if (this.members.size() < 2 && WorldFamilyService.getInstance().getFamily(this.id) != null) {
/* 349 */       WorldFamilyService.getInstance().disbandFamily(this.id);
     }
/* 351 */     this.bDirty = false;
   }

   public void setOnline(int chrId, boolean online, int channel) {
/* 355 */     MapleFamilyCharacter mgc = getMFC(chrId);
/* 356 */     if (mgc != null && mgc.getFamilyId() == this.id) {
/* 357 */       if (mgc.isOnline() != online) {
/* 358 */         broadcast(FamilyPacket.familyLoggedIn(online, mgc.getName()), chrId, (mgc.getId() == this.leaderid) ? null : mgc.getPedigree());
       }
/* 360 */       mgc.setOnline(online);
/* 361 */       mgc.setChannel((byte)channel);
     }
/* 363 */     this.bDirty = true;
   }

   public int setRep(int chrId, int addrep, int oldLevel, String oldName) {
/* 367 */     MapleFamilyCharacter mgc = getMFC(chrId);
/* 368 */     if (mgc != null && mgc.getFamilyId() == this.id) {
/* 369 */       if (oldLevel > mgc.getLevel()) {
/* 370 */         addrep /= 2;
       }
/* 372 */       if (mgc.isOnline()) {
/* 373 */         List<Integer> dummy = new ArrayList<>();
/* 374 */         dummy.add(Integer.valueOf(mgc.getId()));
/* 375 */         broadcast(FamilyPacket.changeRep(addrep, oldName), -1, dummy);
/* 376 */         WorldFamilyService.getInstance().setFamily(this.id, mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep() + addrep, mgc.getTotalRep() + addrep, mgc.getId());
       } else {
/* 378 */         mgc.setCurrentRep(mgc.getCurrentRep() + addrep);
/* 379 */         mgc.setTotalRep(mgc.getTotalRep() + addrep);
/* 380 */         setOfflineFamilyStatus(this.id, mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId());
       }
/* 382 */       return mgc.getSeniorId();
     }
/* 384 */     return 0;
   }

   public MapleFamilyCharacter addFamilyMemberInfo(MapleCharacter player, int seniorid, int junior1, int junior2) {
/* 388 */     MapleFamilyCharacter ret = new MapleFamilyCharacter(player, this.id, seniorid, junior1, junior2);
/* 389 */     this.members.put(Integer.valueOf(player.getId()), ret);
/* 390 */     ret.resetPedigree(this);
/* 391 */     this.bDirty = true;
/* 392 */     List<Integer> toRemove = new ArrayList<>();
/* 393 */     for (int i = 0; i < ret.getPedigree().size(); i++) {
/* 394 */       if (((Integer)ret.getPedigree().get(i)).intValue() != ret.getId()) {


/* 397 */         MapleFamilyCharacter mfc = getMFC(((Integer)ret.getPedigree().get(i)).intValue());
/* 398 */         if (mfc == null) {
/* 399 */           toRemove.add(Integer.valueOf(i));
         } else {
/* 401 */           mfc.resetPedigree(this);
         }
       }
/* 404 */     }  for (Iterator<Integer> iterator = toRemove.iterator(); iterator.hasNext(); ) { int j = ((Integer)iterator.next()).intValue();
/* 405 */       ret.getPedigree().remove(j); }

/* 407 */     return ret;
   }

   public int addFamilyMember(MapleFamilyCharacter mgc) {
/* 411 */     mgc.setFamilyId(this.id);
/* 412 */     this.members.put(Integer.valueOf(mgc.getId()), mgc);
/* 413 */     mgc.resetPedigree(this);
/* 414 */     this.bDirty = true;
/* 415 */     for (Iterator<Integer> iterator = mgc.getPedigree().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 416 */       getMFC(i).resetPedigree(this); }

/* 418 */     return 1;
   }

   public void leaveFamily(int chrId) {
/* 422 */     leaveFamily(getMFC(chrId), true);
   }

   public void leaveFamily(MapleFamilyCharacter mgc, boolean skipLeader) {
/* 426 */     this.bDirty = true;
/* 427 */     if (mgc.getId() == this.leaderid && !skipLeader) {

/* 429 */       this.leadername = null;
/* 430 */       WorldFamilyService.getInstance().disbandFamily(this.id);
     } else {

/* 433 */       if (mgc.getJunior1() > 0) {
/* 434 */         MapleFamilyCharacter j = getMFC(mgc.getJunior1());
/* 435 */         if (j != null) {
/* 436 */           j.setSeniorId(0);
/* 437 */           splitFamily(j.getId(), j);
         }
       }
/* 440 */       if (mgc.getJunior2() > 0) {
/* 441 */         MapleFamilyCharacter j = getMFC(mgc.getJunior2());
/* 442 */         if (j != null) {
/* 443 */           j.setSeniorId(0);
/* 444 */           splitFamily(j.getId(), j);
         }
       }
/* 447 */       if (mgc.getSeniorId() > 0) {
/* 448 */         MapleFamilyCharacter mfc = getMFC(mgc.getSeniorId());
/* 449 */         if (mfc != null) {
/* 450 */           if (mfc.getJunior1() == mgc.getId()) {
/* 451 */             mfc.setJunior1(0);
           } else {
/* 453 */             mfc.setJunior2(0);
           }
         }
       }
/* 457 */       List<Integer> dummy = new ArrayList<>();
/* 458 */       dummy.add(Integer.valueOf(mgc.getId()));
/* 459 */       broadcast(null, -1, FCOp.DISBAND, dummy);
/* 460 */       resetPedigree();
     }
/* 462 */     this.members.remove(Integer.valueOf(mgc.getId()));
/* 463 */     this.bDirty = true;
   }

   public void memberLevelJobUpdate(MapleCharacter mgc) {
/* 467 */     MapleFamilyCharacter member = getMFC(mgc.getId());
/* 468 */     if (member != null) {
/* 469 */       int old_level = member.getLevel();
/* 470 */       int old_job = member.getJobId();
/* 471 */       member.setJobId(mgc.getJob());
/* 472 */       member.setLevel(mgc.getLevel());
/* 473 */       if (old_level != mgc.getLevel()) {
/* 474 */         broadcast(MaplePacketCreator.sendLevelup(true, mgc.getLevel(), mgc.getName()), mgc.getId(), (mgc.getId() == this.leaderid) ? null : member.getPedigree());
       }
/* 476 */       if (old_job != mgc.getJob()) {
/* 477 */         broadcast(MaplePacketCreator.sendJobup(true, mgc.getJob(), mgc.getName()), mgc.getId(), (mgc.getId() == this.leaderid) ? null : member.getPedigree());
       }
     }
   }

   public void disbandFamily() {
/* 483 */     writeToDB(true);
   }

   public MapleFamilyCharacter getMFC(int chrId) {
/* 487 */     return this.members.get(Integer.valueOf(chrId));
   }

   public int getMemberSize() {
/* 491 */     return this.members.size();
   }






   public boolean splitFamily(int splitId, MapleFamilyCharacter def) {
/* 500 */     MapleFamilyCharacter leader = getMFC(splitId);
/* 501 */     if (leader == null) {
/* 502 */       leader = def;
/* 503 */       if (leader == null) {
/* 504 */         return false;
       }
     }
     try {
/* 508 */       List<MapleFamilyCharacter> all = leader.getAllJuniors(this);
/* 509 */       if (all.size() <= 1) {
/* 510 */         leaveFamily(leader, false);
/* 511 */         return true;
       }
/* 513 */       int newId = createFamily(leader.getId());
/* 514 */       if (newId <= 0) {
/* 515 */         return false;
       }
/* 517 */       for (MapleFamilyCharacter mgc : all) {

/* 519 */         mgc.setFamilyId(newId);
/* 520 */         setOfflineFamilyStatus(newId, mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId());
/* 521 */         this.members.remove(Integer.valueOf(mgc.getId()));
       }
/* 523 */       MapleFamily newfam = WorldFamilyService.getInstance().getFamily(newId);
/* 524 */       for (MapleFamilyCharacter mgc : all) {
/* 525 */         if (mgc.isOnline()) {
/* 526 */           WorldFamilyService.getInstance().setFamily(newId, mgc.getSeniorId(), mgc.getJunior1(), mgc.getJunior2(), mgc.getCurrentRep(), mgc.getTotalRep(), mgc.getId());
         }
/* 528 */         newfam.setOnline(mgc.getId(), mgc.isOnline(), mgc.getChannel());
       }
     } finally {
/* 531 */       if (this.members.size() <= 1) {
/* 532 */         WorldFamilyService.getInstance().disbandFamily(this.id);
/* 533 */         return true;
       }
     }
/* 536 */     this.bDirty = true;
/* 537 */     return false;
   }

   public enum FCOp
   {
/* 542 */     NONE, DISBAND;
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\family\MapleFamily.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */